/*
 * Copyright (c) 2010-2020 IISI.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of IISI.
 */
package com.iisigroup.selenium.common;


import java.net.MalformedURLException;
import java.net.URL; 

import org.junit.AfterClass;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

 
/**
 * 這是設計給Selenium 2.43.1以後的版本使用 據說Selenium 3 會完全使用Wb driver
 * 只能使用單一的test
 */
abstract public class AbstractSeleniumTestCase {
    
    /** The Constant logger. */
    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractSeleniumTestCase.class);

    /** The base url. */
    private static URL baseUrl;
    
    /** The driver. */
    public static WebDriver driver; 
    
   
    public static void  setBaseUrl(URL url) throws MalformedURLException{        
        baseUrl =  url ;
    }
    public  static WebDriver getDriver(){
        return driver;
    }
    public  static void setDriver(WebDriver dataDriver){
         driver = dataDriver ;
    }
    /**
     * Before class.
     *
     * @throws Exception the exception
     */

    /**
     * Destroy.
     */
    @AfterClass
    public static void destroy() {
        if (driver != null) {
            LOGGER.info("*** Stopping selenium client driver ...");
            driver.close();
            driver.quit();
        }
    }
    
    /**
     * Open.
     * 相當Selenium 的 open
     *
     * @param url the url
     * @return the string
     */
    public static  String open(String url) {  
        LOGGER.debug("navigateUrl: {}",url);
        driver.navigate().to(url);
        return url.intern();
    }
    /**
     * Checks if is alert present.
     *
     * @return true, if is alert present
     */
    public boolean isAlertPresent() {
	try {
	    driver.switchTo().alert();
	    return true;
	} catch (NoAlertPresentException e) {
	    return false;
	}
    }
}
